<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>销售退货</title>
<link rel="stylesheet" type="text/css"
	href="../extjs/resources/css/ext-all.css" />
<script type="text/javascript" src="../extjs/ext-all.js"></script>
<script type="text/javascript" src="../extjs/ext-lang-zh_CN.js"></script>
<script>
Ext.onReady(function(){
	windowst.show();
	findpage(1);
});
var fielmenu=new Ext.menu.Menu({
		items:[{text:'销售出库转入',handler:function(){
			afronction();
		}}]
	});
	var fielmenu2=new Ext.menu.Menu({
		items:[{text:'单据状况查询'}]
	});
	var fielmenu3=new Ext.menu.Menu({
		items:[{text:'批号设定'},{text:'条码打印'},{text:'批次变更单价'}]
	});
	
	var fielmenu4=new Ext.menu.Menu({
		items:[{text:'新建',handler:function(){
			empty();
		}},{text:'保存',handler:function(){
			add();
		}},{text:'修改',handler:function(){
			add();
		}},{text:'删除',handler:function(){
			del();
		}},{text:'审核',handler:function(){
			auditing();
		}},{text:'反审核',handler:function(){
			
		}}]
	});
	
	var grids=Ext.create('Ext.grid.Panel',{
		 listeners:{
			containerdblclick: function(){ //双击事件
		    	this.getStore().add({
		    		'prodId':'',
		    		'prodName':'',
		    		'prodsize':'',
		    		'danwei':'',
		    		'sumqty':'',
		    		'beforemoney':'',
		    		'zheshu':'',
		    		'price':'',
		    		'money':'',
		    		'taxlv':'',
		    		'taxmoney':'',
		    		'sumtax':'',
		    		'trantype':'',
		    		'fromno':''
		    	});				
	    	},edit:function(){
	    		jisui();
	        }
		},plugins: [
   				Ext.create('Ext.grid.plugin.CellEditing', {
       				clicksToEdit: 2
   				})
		],
		columns: [{
					header: '（栏号）',
               		xtype: 'rownumberer',
               		dataIndex: 'lh', 
               		width: 50,
					sortable: false
    			 },
				 { header: '物料编号',  dataIndex: 'prodId', width: 150,
					 editor: {xtype:'textfield',
// 						 listeners:{
// 							 focus:function(grid, e, eOpts){
// 								 windows2.show();
// 							 }	
// 						 }
					  } 
				 },
	       		 { header: '（物料名称）', dataIndex: 'prodName',width: 170,editor:"textfield"},
   				 { header: '（规格型号）', dataIndex: 'prodsize',editor:"textfield"},
				 { header: '（单位名称）', dataIndex: 'danwei',editor:"textfield"},
				 { header: '数量', dataIndex: 'sumqty',editor:"textfield"},
				 { header: '折扣前单价', dataIndex: 'beforemoney',editor:"textfield"},
				 { header: '折数(%)', dataIndex: 'zheshu',editor:"textfield"},
				 { header: '单价', dataIndex: 'price', width:120,editor:"textfield"},
				 { header: '（金额）', dataIndex: 'money',width:120,editor:"textfield"},
				 { header: '税率', dataIndex: 'taxlv',editor:"textfield"},
				 { header: '税额', dataIndex: 'taxmoney',editor:"textfield"},
				 { header: '含税金额', dataIndex: 'sumtax',editor:"textfield"},
				 { header: '（来源单别）', dataIndex: 'trantype',editor:"textfield"},
				 { header: '（来源单号）', dataIndex: 'fromno',width: 150,editor:"textfield"}
				 ],
	   			 height: 130,
				 width:1700,
				 autoScroll:false,
				 stripeRows:true
	});
	var grids1=Ext.create('Ext.grid.Panel',{
		listeners:{
			'itemdblclick' : function(grid, rowIndex, e){
				var record = grid.getSelectionModel().getSelection();
				var srdid =record[0].get("billNo");
				loadck(srdid);
				windows3.close();
			}  		
	    },columns:[{
					header: '（栏号）',
              		xtype: 'rownumberer',
              		dataIndex: 'lh', 
              		width: 50,
					sortable: false
   			 	 },
				 { header:'出库单号',dataIndex:'billNo',width:140},
	       		 { header:'出库时间',dataIndex:'billDate',width:120},
  				 { header:'客户名称',dataIndex:'custID'}],
	   			 height:268,
				 width:388,
				 autoScroll:false,
				 stripeRows:true
	});
	
	var filterPanel = Ext.create('Ext.panel.Panel', {
	    bodyPadding: 6,  // 避免Panel中的子元素紧邻边框
	    width: 1640,
		height:170,
	    items:[grids],
		bbar:[{xtype:"label",text:'总计:',width:90},
		{xtype:"textfield",id:'number',disabled:true,width:90,margin:'0 0 0 240',value:'0.00'},
		{xtype:"textfield",id:'jine',disabled:true,width:90,margin:'0 0 0 166',value:'0.00'},
		{xtype:"textfield",id:'suie',disabled:true,width:90,margin:'0 6 0 64',value:'0.00'},
		{xtype:"textfield",id:'hansuijine',disabled:true,width:90,margin:'0 0 0 0',value:'0.00'}],
	});
	
	var formsz=Ext.create('Ext.form.Panel', {
	    bodyPadding: 5,
		height:'100%',
		layout:"column",
		baseCls:'x-plain',
		items:[
		{
			id:'dueto',
			style:'margin-left:10px;color:blue',
			xtype: 'textfield',
	        name: 'dueto',
	        fieldLabel: '账款归属',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
		},{
			id:'prepayday',
			style:'margin-left:80px',
			xtype: 'datefield',
	        name: 'prepayday',
	        fieldLabel: '收款日期',
	        value:Ext.util.Format.date(Ext.Date.add(new Date(),Ext.Date.MONTH,0),"Y-m-d"),
	        format : 'Y-m-d',
			width:250,
	        labelWidth: 70,
	        anchor: '100%',
		},{
			style:'margin-left:10px',
			xtype: 'combo',
	        name: '',
	        fieldLabel: '收款条件',
			width:140,
	        labelWidth: 80,
	        anchor: '100%',
	        name: 'IncludeRate',
	        displayField: 'name',
			valueField: 'abbr',
			store: Ext.create('Ext.data.Store', {
			fields: ['abbr', 'name'],
			data : [
				{"abbr":"货到", "name":"货到"},
				{"abbr":"次月", "name":"次月"},
				{"abbr":"月结", "name":"月结"},
				{"abbr":"其他", "name":"其他"}
		    ]
			}),
			value: '货到',
			typeAhead: true,
			mode: 'local',
			triggerAction: 'all',
			selectOnFocus:true,
		},{
			xtype: 'textfield',
	        name: 'dizhiton',
			width:94,
	        labelWidth: 70,
			margin:"0 1",
	        anchor: '100%',
		},{
			xtype: 'label',
	        text: '天',
			width:90,
	        labelWidth: 70,
			margin:"2 3",
	        anchor: '100%',
		}]
	});
	
	var formsg=Ext.create('Ext.form.Panel', {
	    bodyPadding: 5,
		height:'100%',
		layout:"column",
		baseCls:'x-plain',
		items:[{
			id:'salesid',
			xtype: 'textfield',
	        name: 'salesid',
			bodyPadding: 5,
	        fieldLabel: '业务人员',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
		},{
			id:'maker',
			xtype: 'textfield',
	        name: 'maker',
	        fieldLabel: '制单人员',
			style:'margin-left:19px',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
		},{
			id:'departid',
			xtype: 'textfield',
	        name: 'departid',
	        fieldLabel: '所属部门',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
		},{
			id:'permitter',
			xtype: 'textfield',
	        name: 'permitter',
	        fieldLabel: '复核人员',
			style:'margin-left:19px',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
		},{
			id:'projectid',
			xtype: 'textfield',
	        name: 'projectid',
	        fieldLabel: '所属项目',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
		}],
	});
	
var tableds=Ext.create('Ext.TabPanel',{
	width:"100%",
	height:230,
	autoScroll:true,
	bodyPadding: 5,
	items:[{title:'内容',autoScroll:true,items:[filterPanel]},{title:'账款',autoScroll:true,items:[formsz]},{title:'备注',xtype:'textarea',name:'Remark',fieldLabel: '备注',labelWidth: 30,}]
});

var forms=Ext.create('Ext.form.Panel', {
    bodyPadding: 5,
    id:'frm',
	height:'100%',
	layout:"column",
	baseCls:'x-plain',
	items:[
	{
		id:'custoshortid',
		style:'margin-left:20px',
		xtype: 'textfield',
        name: 'custoshortid',
        fieldLabel: '客户',
		width:250,
        labelWidth: 80,
		emptyText:'请输入客户',
        anchor: '100%',
	},{
		id:'billdate',
		style:'margin-left:80px;color:blue',
		xtype: 'datefield',
        name: 'billdate',
        fieldLabel: '单据日期',
        format : 'Y-m-d',
        value:Ext.util.Format.date(Ext.Date.add(new Date(),Ext.Date.MONTH,0),"Y-m-d"),
		width:250,
        labelWidth: 70,
        listeners: {
        	 'blur': function() { loadid(); }
        },
        anchor: '100%'
	},{
		id:'addrid',
		style:'margin-left:20px',
		xtype: 'textfield',
        name: 'addrid',
        fieldLabel: '送货地址',
		width:150,
        labelWidth: 80,
        anchor: '100%',
	},{
		id:'custaddress',
		xtype: 'textfield',
        name: 'custaddress',
		width:99,
        labelWidth: 70,
		margin:"0 1",
        anchor: '100%',
	},{
		id:'billno',
		xtype: 'textfield',
        name: 'billno',
		style:"color:blue;margin-left:80px",
        fieldLabel: '单据号码',
		width:250,
        labelWidth: 70,
        anchor: '100%',
	},{
		id:'priceoftax',
		style:'margin-left:20px',
        fieldLabel: '单价是否含税',
		xtype:'combo',
        name: 'priceoftax',
        displayField: 'name',
		labelWidth:80,
		width:250,
		valueField: 'abbr',
		store: Ext.create('Ext.data.Store', {
		fields: ['abbr', 'name'],
		data : [
			{"abbr":"含税", "name":"含税"},
			{"abbr":"未税", "name":"未税"}
	    ]
		}),
		value: '含税',
		typeAhead: true,
		mode: 'local',
		triggerAction: 'all',
		selectOnFocus:true,
     },{
    	id:'currname',
		style:'margin-left:80px',
		xtype: 'textfield', //多行文本域
        name: 'currname',
        fieldLabel: '币别',
        labelWidth: 70,
		width:250,
        anchor: '100%',
	},{
		id:'wareid',
	    xtype: 'textfield',
        name: 'wareid',
        fieldLabel: '仓库',
		width:250,
		style:"color:blue;margin-left:20px",
        labelWidth: 80,
        anchor: '100%',
	},{
		id:'exchrate',
		style:'margin-left:80px',
	 	xtype: 'textfield', //多行文本域
        name: 'exchrate',
        fieldLabel: '汇率',
        labelWidth: 70,
		width:250,
        anchor: '100%',
	},{
		id:'isdeduct',
		style:'margin-left:20px',
       	fieldLabel: '重新出库',
		xtype:'combo',
       	name: 'isdeduct',
       	displayField: 'name',
		labelWidth:80,
		width:250,
		valueField: 'abbr',
		store: Ext.create('Ext.data.Store', {
		fields: ['abbr', 'name'],
		data : [
			{"abbr":"是", "name":"是"},
			{"abbr":"否", "name":"否"}
	    ]
		}),
		value: '否',
		typeAhead: true,
		mode: 'local',
		triggerAction: 'all',
		selectOnFocus:true,	
    },{
        fieldLabel: '国外贸易',
		xtype:'combo',
       	name: 'guowaimaoyi',
		style:"margin-left:80px",
       	displayField: 'name',
       	id:'ifIdValue',
		labelWidth:70,
		width:250,
// 		valueField: 'abbr',
// 		store: Ext.create('Ext.data.Store', {
// 		fields: ['abbr', 'name'],
// 		data : [
// 			{"abbr":"是", "name":"是"},
// 			{"abbr":"否", "name":"否"}
// 	    ]
// 		}),
		value: '否',
		typeAhead: true,
		mode: 'local',
		triggerAction: 'all',
		selectOnFocus:true,
		typeAhead:false,
	    editable:false
   	},{
	    style:'margin-left:20px',
		xtype: 'textfield',
        name: 'ProveBillNo',
        fieldLabel: '凭证单号',
		width:250,
        labelWidth: 80,
        anchor: '100%',
	},tableds,formsg]
});

Ext.getCmp('ifIdValue').disable();

//winform窗口
var windowst=new Ext.Window({
	width:640,
	height:570,
	title:"销售退货单",
	closable:true,
	resizable:false, //设置是否可以改变大小
	draggable:true,
	anchor: '100%',
	items:[forms],
	bbar:[{xtype:"splitbutton",text:'转单',width:90,menu:fielmenu},
	{xtype:"splitbutton",text:'查询',width:90,menu:fielmenu2},
	{xtype:"splitbutton",text:'功能',width:90,menu:fielmenu3},
	{xtype:"splitbutton",text:'操作',width:90,menu:fielmenu4},
	{xtype:"splitbutton",text:'上一页',width:90,handler:function(){
		findpage(pb.previouspage);
	}},
	{xtype:"splitbutton",text:'下一页',width:90,handler:function(){
		findpage(pb.nextpage);
	}}],
});

var windows2=new Ext.Window({
	width:400,
	height:300,
	title:"物料选择",
	closable:true,
	resizable:false, //设置是否可以改变大小
	draggable:true,
	closeAction:"hide",
});

var windows3=new Ext.Window({
	width:400,
	height:300,
	title:"选择销售出库单",
	closable:true,
	resizable:false, //设置是否可以改变大小
	draggable:true,
	closeAction:"hide",
	items:[grids1]
});

//转出库单
function  afronction(){
// 	var song=Ext.encode(forms.getForm().getValues());
	windows3.show();
	Ext.Ajax.request({  
        //被用来向服务器发起请求默认的url  
        url : "/salesre/querychuku",  
        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串  
//         params :{pagenum:num},  
        //请求时使用的默认的http方法  
        method : "post",  
        //请求成功时回调函数  
        success : function(data) {
        	ck=Ext.JSON.decode(data.responseText)
        	grids1.store.removeAll();
    		grids1.getStore().add(ck);
        },
        //请求失败时回调函数  
        failure : function() {  
        	Ext.MessageBox.alert("信息提示", "信息删除出错，请联系管理员!",function(){});  
        }  
	});   
}

//分页
var pb = null;
function findpage(num){
	Ext.Ajax.request({  
        //被用来向服务器发起请求默认的url  
        url : "/salesre/query",  
        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串  
        params :{pagenum:num},  
        //请求时使用的默认的http方法  
        method : "post",  
        //请求成功时回调函数  
        success : function(data) {
        	pb=Ext.JSON.decode(data.responseText)
			if(pb==null){
				loadid();
        		return;
        	}else {
        		grids.store.removeAll();
            	Ext.getCmp('billno').setValue(pb.pagedata[0].billno);
    			Ext.getCmp('custoshortid').setValue(pb.pagedata[0].custoshortid);
    			Ext.getCmp('billdate').setValue(pb.pagedata[0].billdate);
    			Ext.getCmp('addrid').setValue(pb.pagedata[0].addrid);
    			Ext.getCmp('custaddress').setValue(pb.pagedata[0].custaddress);
    			Ext.getCmp('priceoftax').setValue(pb.pagedata[0].priceoftax);
    			Ext.getCmp('currname').setValue(pb.pagedata[0].currname);
    			Ext.getCmp('wareid').setValue(pb.pagedata[0].wareid);
    			Ext.getCmp('exchrate').setValue(pb.pagedata[0].exchrate);
    			Ext.getCmp('salesid').setValue(pb.pagedata[0].salesid);
    			Ext.getCmp('departid').setValue(pb.pagedata[0].departid);
    			Ext.getCmp('maker').setValue(pb.pagedata[0].maker);
    			Ext.getCmp('permitter').setValue(pb.pagedata[0].permitter);
    			Ext.getCmp('prepayday').setValue(pb.pagedata[0].prepayday);
    			Ext.getCmp('dueto').setValue(pb.pagedata[0].dueto);
    			Ext.getCmp('isdeduct').setValue(pb.pagedata[0].isdeduct);
    			Ext.getCmp('projectid').setValue(pb.pagedata[0].projectid);
//     			Ext.getCmp('memo').setValue(pb.pagedata[0].memo);
    			grids.getStore().add(pb.pagedata[0].salesreturnDetails);
    			jisui();
        	}
        },
        //请求失败时回调函数  
        failure : function() {  
        	Ext.MessageBox.alert("信息提示", "信息删除出错，请联系管理员!",function(){});  
        }  
	});  
}

//保存
function add(){
	var salesre={};
	salesre['billno'] = Ext.getCmp('billno').getValue();
	salesre['custoshortid'] = Ext.getCmp('custoshortid').getValue();
	salesre['billdate'] = Ext.Date.format(Ext.getCmp('billdate').getValue(),'Y-m-d');
	salesre['addrid'] = Ext.getCmp('addrid').getValue();
	salesre['custaddress'] = Ext.getCmp('custaddress').getValue();
	salesre['priceoftax'] = Ext.getCmp('priceoftax').getValue();
	salesre['currname'] = Ext.getCmp('currname').getValue();
	salesre['wareid'] = Ext.getCmp('wareid').getValue();
	salesre['exchrate'] = Ext.getCmp('exchrate').getValue();
	salesre['salesid'] = Ext.getCmp('salesid').getValue();
	salesre['departid'] = Ext.getCmp('departid').getValue();
	salesre['maker'] = Ext.getCmp('maker').getValue();
	salesre['permitter'] = Ext.getCmp('permitter').getValue();
	salesre['prepayday'] = Ext.Date.format(Ext.getCmp('prepayday').getValue(),'Y-m-d');
	salesre['dueto'] = Ext.getCmp('dueto').getValue();
	salesre['isdeduct'] = Ext.getCmp('isdeduct').getValue();
	salesre['projectid'] = Ext.getCmp('projectid').getValue();
	salesre['total'] = Ext.getCmp('hansuijine').getValue();
	salesre['salesreturnDetails'] = Ext.pluck(grids.getStore().data.items,'data');
	alert(JSON.stringify(salesre));
	Ext.Ajax.request({  
        //被用来向服务器发起请求默认的url  
        url : "/salesre/add",
        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串 
       	headers: {'Content-Type':'application/json'},
        params :JSON.stringify(salesre),  
        //请求时使用的默认的http方法  
        method : "post",
        //请求成功时回调函数  
        success : function(data) {
            Ext.MessageBox.alert("信息提示", "成功新增记录！",function(){});  
        },
        //请求失败时回调函数 
        failure : function() {  
        	Ext.MessageBox.alert("信息提示", "信息删除出错，请联系管理员!",function(){});  
        }  
	});  
}

//审核
function auditing(){
	Ext.Ajax.request({   
        url : "/salesre/thauditing",
        params :{id:Ext.getCmp('billno').getValue()},  
        method : "post",
        success : function(data) {
        	Ext.MessageBox.alert("信息提示", "审核成功！",function(){});
        	findpage(1);
        },
        failure : function() {  
        	Ext.MessageBox.alert("信息提示", "信息删除出错，请联系管理员!",function(){});  
        }  
	});
}

//删除
function del(){
	Ext.Ajax.request({  
        //被用来向服务器发起请求默认的url  
        url : "/salesre/del",
        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串 
//        	headers: {'Content-Type':'application/json'},
        params :{id:Ext.getCmp('billno').getValue()},  
        //请求时使用的默认的http方法  
        method : "post",
        //请求成功时回调函数  
        success : function(data) {
        	Ext.MessageBox.alert("信息提示", "删除成功！",function(){});
        	findpage(1);
        },
        //请求失败时回调函数
        failure : function() {  
        	Ext.MessageBox.alert("信息提示", "信息删除出错，请联系管理员!",function(){});  
        }  
	});
}

//加载单号
function loadid(){
	Ext.Ajax.request({  
        //被用来向服务器发起请求默认的url  
        url : "/salesre/getid",
        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串 
//        	headers: {'Content-Type':'application/json'},
        params :{date:Ext.Date.format(Ext.getCmp('billdate').getValue(),'Y-m-d')},  
        //请求时使用的默认的http方法  
        method : "post",
        //请求成功时回调函数  
        success : function(data) {
        	Ext.getCmp('billno').setValue(data.responseText);
//             Ext.ux.Toast.msg("信息提示", "成功新增记录！");  
        },
        //请求失败时回调函数
        failure : function() {  
        	Ext.MessageBox.alert("信息提示", "信息删除出错，请联系管理员!",function(){});  
        }  
	});
}

//加载出库单
function loadck(id){
	Ext.Ajax.request({  
        //被用来向服务器发起请求默认的url  
        url : "/salesre/loadchuku",  
        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串  
        params :{ckid:id},  
        //请求时使用的默认的http方法  
        method : "post",  
        //请求成功时回调函数  
        success : function(data) {
        	ck=Ext.JSON.decode(data.responseText)
       		grids.store.removeAll();
   			Ext.getCmp('custoshortid').setValue(ck.custID);
   			Ext.getCmp('addrid').setValue(ck.custAddress);
   			Ext.getCmp('custaddress').setValue(ck.custAddress);
   			Ext.getCmp('priceoftax').setValue(ck.pricefTax);
   			Ext.getCmp('currname').setValue(ck.currName);
   			Ext.getCmp('wareid').setValue(ck.wareID);
   			Ext.getCmp('exchrate').setValue(ck.exchRate);
   			Ext.getCmp('salesid').setValue(ck.salesID);
   			Ext.getCmp('departid').setValue(ck.departID);
   			Ext.getCmp('maker').setValue(ck.maker);
   			Ext.getCmp('permitter').setValue(ck.permitter);
//     		Ext.getCmp('memo').setValue(pb.pagedata[0].memo);
   			grids.getStore().add(ck.info);
   			jisui();
   			loadid();
        },
        //请求失败时回调函数  
        failure : function() {  
        	Ext.MessageBox.alert("信息提示", "信息删除出错，请联系管理员!",function(){});  
        }  
	});  
}

//清空
function empty(){
	Ext.getCmp('frm').form.reset();
	grids.store.removeAll();
	loadid();
}

//grid的计算
function jisui(){
	var number=0;
	var jine=0.00;
	var suie=0.00;
	var hansuijine=0.00;
	Ext.each(Ext.pluck(grids.getStore().data.items,'data'), function(obj,i) {  
		number+=parseInt(obj.sumqty);
		jine+=parseFloat(obj.money);
		suie+=parseFloat(obj.taxmoney);
		hansuijine+=parseFloat(obj.sumtax);
    });
	Ext.getCmp('number').setValue(number);
	Ext.getCmp('jine').setValue(jine);
	Ext.getCmp('suie').setValue(suie);
	Ext.getCmp('hansuijine').setValue(hansuijine);
}

</script>
</head>

<body>
</body>
</html>